import Vue from "vue";
import VueRouter from "vue-router";
import login from "../views/login.vue";

import unit from "../views/unit.vue";
import itemType from "../views/itemType.vue";
import depository from "../views/depository.vue";
import userType from "../views/userType.vue";
import process from "../views/process.vue";
import project from "../views/project.vue";

import billIn from "../views/bill/in.vue";
import billOut from "../views/bill/out.vue";
import billIO from "../views/bill/io.vue";
import query from "../views/query.vue";
Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    component: login
  },
  { path: '/', redirect: '/login' },
  {
    path: '/admin',
    component: () => import("../views/admin.vue"),
    children: [
      { path: '/admin/unit', component: unit },
      { path: '/admin/itemType', component: itemType },
      { path: '/admin/depository', component: depository },
      { path: '/admin/userType', component: userType },
      { path: '/admin/process', component: process },
      { path: '/admin/project', component: project },
      { path: '/admin/in/:id', component: billIn },
      { path: '/admin/out/:id', component: billOut },
      { path: '/admin/io/:id', component: billIO },
      { path: '/admin/query', component: query },
    ]
  }
];

const router = new VueRouter({
  routes
});
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    return next();
  }
  else if (to.path === '/login') return next();
  else if (to.path.substr(0, 6) == '/admin') {
    if (sessionStorage.getItem('token') && sessionStorage.getItem('token') != 'undefined') return next();
    return next('/login');
  }
})

export default router;
